package com.west.xu.sms.util;

import okhttp3.*;

import java.io.IOException;
import java.util.Map;

public class SendSmsUtil {
    public static final MediaType JSON
            = MediaType.parse("application/json; charset=utf-8");

    public static String sendGet(String url) throws IOException {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }

    public static String sendPost(String url, String json, Map<String, String> headerMap) throws IOException {
        OkHttpClient client = new OkHttpClient();

        RequestBody body = RequestBody.create(JSON, json);

        Request.Builder builder = new Request.Builder();
        builder.url(url);
        for (Map.Entry<String, String> params : headerMap.entrySet()) {
            builder.addHeader(params.getKey(), params.getValue());
        }
        builder.post(body);
        Request request = builder.build();

        Response response = client.newCall(request).execute();
        return response.body().string();
    }
}
